package com.ocom.human.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ocom.common.dto.GetCustomRolePowerDto;
import com.ocom.common.entity.human.EntityRolePermission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface IDaoRolePermission extends BaseMapper<EntityRolePermission> {

    @Select({"<script>",
            "select  r.*   " +
                    "FROM role_permission r left join company_role c on r.role=c.role_id  " +
                    "where c.com_id=#{comId} " +
                    "and r.api_id=0 " +
                    " <when test=\"pageId &gt; 0 \" > "+
                         " AND r.page_id = #{pageId} "+
                    " </when> "+
                    "order by page_id" +
                    "</script>"})
    public List<EntityRolePermission> selectRolePagePermission(Long comId, Integer role, Integer pageId);

    @Select({"<script>",
            "select  r.*   " +
                    "FROM role_permission r left join company_role c on r.role=c.role_id  " +
                    "where c.com_id=#{comId} " +
                    "and r.api_id &gt; 0 " +
                    " AND r.page_id = #{pageId} "+
                    "order by api_id" +
                    "</script>"})
    public List<EntityRolePermission> getRoleButtonPermission(Long comId, Integer role, Integer pageId);

    @Select({"<script>",
            " SELECT " +
            " CASE " +
            " WHEN api_id = 0 THEN " +
            " page_id " +
            " ELSE " +
            " api_id " +
            " END Id, " +
            " CASE " +
            " WHEN api_id = 0 THEN " +
            " 0 " +
            " ELSE " +
            " 1 " +
            " END type, " +
            " permission " +
            " FROM " +
            " role_permission " +
            " WHERE " +
            " role = #{role} " +
            " AND permission &gt; 0 " +
            " ORDER BY " +
            " page_id, " +
            " api_id" +
            "</script>"})
    List<GetCustomRolePowerDto> getCustomRolePower(@Param("role") Integer role, @Param("comId") Integer comId);
}
